package com.yule.mq.consumer;

import com.yule.config.DynamicDataSourceContext;
import com.yule.system.home.SystemLogHome;
import com.yule.system.model.SystemLog;
import com.yule.utils.UserUtils;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;

@Component
public class SystemLogConsumer {

    @Autowired
    private SystemLogHome systemLogHome;

    @JmsListener(destination = "SystemLog")
    public void receiveQueue(Message message) {
        ActiveMQObjectMessage activeMQObjectMessage = (ActiveMQObjectMessage) message;
        try {
            SystemLog systemLog = (SystemLog) activeMQObjectMessage.getObject();
            DynamicDataSourceContext.setDBType(systemLog.getCorpLoginName());
            systemLogHome.save(systemLog);
            activeMQObjectMessage.acknowledge();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
